﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ThirdClassAssignment{
    public class FileParse{

        private FileParse(){
            
        }

        private static bool IsLetter(char c){
            return Char.IsLower(c) || Char.IsUpper(c);
        }

        public static Dictionary<string, int> CountWords(string content){

            var words = new Dictionary<string, int>();
            var begin = 0;
            var end = begin;
            for (;end<content.Length;end++) {
                if (!IsLetter(content[end])) {
                    var str = content.Substring(begin, end - begin);
                    if (str == ""){}
                    else if(words.ContainsKey(str))
                        words[str]++;
                    else
                        words.Add(str,1);
                    begin = end + 1;
                }
            }
            return words;
        }
        
        
        public static string Read(string path){
            var content = new StringBuilder();
            var stream = new StreamReader(path);
            string line;
            while ((line = stream.ReadLine()) != null) {
                content.AppendLine(line);
            }
            //关闭流
            stream.Close();
            return content.ToString();
        }

        private static bool IsExegesis(string line){
            var trim = line.Trim();
            return trim.Length > 1 && trim[0] == '/' && trim[1] == '/';
        }

        public static string LineDelete(string content){
            var result = new StringBuilder();
            var lines = content.Split('\n');
            foreach (var line in lines) {
                if(line.Trim().Length == 0 || IsExegesis(line) )
                    continue;
                result.AppendLine(line);
            }
            return result.ToString();
        }
        
    }
}